package org.his.controller.kaiyao;

import org.his.dao.*;
import org.his.pojo.caiwu.PatmentInfo;
import org.his.pojo.kaiyao.Kaiyao;
import org.his.pojo.kaiyao.KaiyaoInfo;
import org.his.pojo.yaopin.Ypgl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/kaiyao")
public class KaiyaoController {

    @Autowired
    private KaiYaoDao kaiYaoDao;

    @Autowired
    private KaoYaoInfoDao kaoYaoInfoDao;

    @Autowired
    private YpglDao ypglDao;

    @Autowired
    private PatmentInfoDao patmentInfoDao;

    @Autowired
    private PatmentDao patmentDao;

    @RequestMapping("/addKaiYao")
    @ResponseBody
    public Map<String,Object> addKaiyao(Kaiyao kaiyao)throws Exception{
        Map<String,Object> map = new HashMap<>();

        kaiYaoDao.add(kaiyao);
        map.put("msg",kaiyao.getKyId());
//        病例号
        map.put("bingLiNum",kaiyao.getBingLiNum());
        return map;
    }
    @RequestMapping("/addKaiYaoInfo")
    @ResponseBody
    public Map<String,Object> addKaiyao(KaiyaoInfo kaiyaoInfo, String caseNo)throws Exception{
        Map<String,Object> map = new HashMap<>();

        kaoYaoInfoDao.add(kaiyaoInfo);

//        药品实体
        Ypgl ypgl =ypglDao.get(kaiyaoInfo.getYpid());
        int ypkc = ypgl.getYpkc() - kaiyaoInfo.getYpNum();

//        修改药品的库存
        ypglDao.updkcNum(ypgl.getYpid(),ypkc);
//        消费记录表中添加数据
        PatmentInfo patmentInfo = new PatmentInfo();

//        财务表中该患者的id
        Integer payId = patmentDao.getMemberCaseNo(Integer.parseInt(caseNo),0);

        System.out.println("病例号"+caseNo);
        System.out.println("财务表中id："+payId);



        patmentInfo.setPayId(payId);
        patmentInfo.setNumber(kaiyaoInfo.getYpNum());
        patmentInfo.setOption(ypgl.getYpname());
//        价格
        int money = Integer.parseInt(ypgl.getYplsj())* kaiyaoInfo.getYpNum();
        patmentInfo.setMoney(money);


        patmentInfoDao.add(patmentInfo);

        return map;
    }


}
